package com.infore.common.utils;

import cn.hutool.core.io.FileTypeUtil;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
 * @author shaolay
 * @date 2022年05月06日 15:40
 */
public class FileTypeUtils {

    private static List<String> image = Arrays.asList("BMP", "JPEG", "JPG", "PNG", "TIFF", "GIF", "PCX", "TGA", "EXIF", "FPX ", "SVG", "PS");
    private static List<String> audio = Arrays.asList("MP3", "WMA", "RM", "WAV", "MID", "APE", "FLAC", "AMR");
    private static List<String> video = Arrays.asList("MPG", "MP4", "MPEG", "AVI", "RM", "RMVB", "MOV", "WMV", "ASF", "DAT", "ASX", "WVX", "MPE", "MPA");


    public static String getType(File file){
        String type = FileTypeUtil.getType(file).toUpperCase(Locale.ROOT);
        if (image.contains(type)){
            return "image";
        }else if (audio.contains(type)){
            return "audio";
        }else if (video.contains(type)){
            return "video";
        }
        return "text";
    }

    public static String getType(String fileName){
        String suffixName = "";
        if (fileName != null){
            suffixName = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : null;
            if (suffixName != null){
                suffixName = suffixName.toUpperCase(Locale.ROOT);
            }else {
                return "text";
            }
            if (image.contains(suffixName)){
                return "image";
            }else if (audio.contains(suffixName)){
                return "audio";
            }else if (video.contains(suffixName)){
                return "video";
            }
            return "text";
        }else {
            return "text";
        }
    }

    public static void main(String[] args) {
        System.out.println(getType("file.png"));
    }
}
